//
//  JZHomeViewController.m
//  NuoMi
//
//  Created by liangyi on 15/12/3.
//  Copyright © 2015年 liangyi. All rights reserved.
//

#import "JZHomeViewController.h"
#import "HomeMenuTableViewCell.h"
#import "JZNetworkSingleton.h"
#import "JZHomepageModel.h"
#import "JZHomeNewCell.h"
#import "JZHomeShopModel.h"
#import "JZHomeJingxuanCell.h"
#import "JZMovieViewController.h"
#import "JZAlbumCell.h"
#import "JZHomeBlock2Cell.h"
#import "JZHomeShopCell.h"
#import "JZNuomiHeader.h"
@interface JZHomeViewController ()<UITableViewDataSource, UITableViewDelegate, HomeMenuDelegate, HOmeJingxuanDelegate, JZHomeBlock2Delegate, JZAlbumDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, strong) NSMutableArray *likeArray;
@property (nonatomic, strong) NSMutableArray *bannersArray;
@property (nonatomic, strong) JZHomepageModel *homepageM;
@property (nonatomic, strong) NSMutableArray *userBlockArray;
@property (nonatomic, strong) NSMutableArray *nuomiChannelArray;
@property (nonatomic, strong) NSMutableArray *categoryArray;
@property (nonatomic, strong) NSMutableArray *recommendArray;
@property (nonatomic, strong) JZHometoptenModel *topenModel;
@property (nonatomic, strong) JZHomespecialModel *specialModel;


@end

@implementation JZHomeViewController
//- (void)viewWillAppear:(BOOL)animated
//{
//      self.edgesForExtendedLayout = UIRectEdgeNone;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self initData];
    
    [self setUpTableView];
    
    
    // Do any additional setup after loading the view.
}
-(void)setUpTableView{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    //    self.tableView.header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableview.mj_header = [JZNuomiHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 马上进入刷新状态
    [self.tableview.mj_header beginRefreshing];
}
-(void)loadNewData{
    [self getHotData];
    [self getRecommendData];
}


//获取数据
-(void)getHotData {
    //    url内部添加有签名加密，所以只能把url写死，不能动态的改变url
    NSString *url = @"http://app.nuomi.com/naserver/home/homepage?appid=ios&bduss=&channel=com_dot_apple&cityid=1300010000&cuid=c538d1feea07a5add5c7107e31db87dbbcba3605&device=iPhone&ha=5&lbsidfa=3F90F85F-CB89-4CE1-A54F-EB64F4D304C0&location=38.865220%2C121.535260&logpage=Home&net=unknown&os=8.4&sheight=1334&sign=9f076cd26daaae04ad1deeee6473b05c&swidth=750&terminal_type=ios&timestamp=1449561740131&tn=ios&uuid=c538d1feea07a5add5c7107e31db87dbbcba3605&v=6.0.2";
    JZNetworkSingleton *request = [JZNetworkSingleton request];
    request.classModel = @"JZHomepageModel";
    
    __weak typeof(self) weakself = self;
    [request getDataWithURL:url params:nil success:^(OPDataResponse *responseObject) {

        if (responseObject.error) {
            NSLog(@"error:  %@",responseObject.error);
            [SVProgressHUD showInfoWithStatus:responseObject.error.description];
            [_tableview.mj_header endRefreshing];
            return ;
        }
        _homepageM = responseObject.data;
        
       
        
        
        _bannersArray = [[NSMutableArray alloc] initWithArray:_homepageM.banners];
        _categoryArray = [[NSMutableArray alloc] initWithArray:_homepageM.category];
        _recommendArray = [[NSMutableArray alloc] initWithArray:_homepageM.recommend];
//        _specialModel = _homepageM.special;
        _userBlockArray = [[NSMutableArray alloc]initWithArray:_homepageM.userBlock];
        _nuomiChannelArray = [[NSMutableArray alloc]initWithArray:_homepageM.nuomiChannel];
        _topenModel = _homepageM.topten;
//

        
        [weakself.tableview reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableview.mj_header endRefreshing];
//                NSLog(@"special===:   %@",_homepageM.special);
//                NSLog(@"topten===:   %@",_homepageM.topten);
        
        
//        [self createTable];
    } failure:^(NSError *error) {
        NSLog(@"获取 首页 数据失败");
        // 拿到当前的下拉刷新控件，结束刷新状态
        [SVProgressHUD showErrorWithStatus:@"网络连接失败"];
        [self.tableview.mj_header endRefreshing];
    }];
}
//获取猜你喜欢数据
-(void)getRecommendData {
    
    NSString *url = @"http://app.nuomi.com/naserver/search/likeitem?appid=ios&bduss=&channel=com_dot_apple&cityid=1300010000&cuid=c538d1feea07a5add5c7107e31db87dbbcba3605&device=iPhone&frontend_style=singleList&ha=5&last_s=-1&lbsidfa=3F90F85F-CB89-4CE1-A54F-EB64F4D304C0&locate_city_id=1300010000&location=38.865120%2C121.535310&logpage=Home&net=wifi&os=8.4&page_type=native&sheight=1334&sign=fdc95cd084a8325312dbe81964d4b1c7&sort_type=0&start_idx=0&swidth=750&terminal_type=ios&timestamp=1449623850488&tn=ios&total_page=1&tuan_size=20&uuid=c538d1feea07a5add5c7107e31db87dbbcba3605&v=6.0.3";
    JZNetworkSingleton *request = [JZNetworkSingleton request];
    request.classModel = @"JZHomeShopModel";
    __weak typeof(self) weakself = self;
    [request getDataWithURL:url params:nil success:^(OPDataResponse *responseObject){
        NSLog(@"获取 猜你喜欢 数据成功");
        if (responseObject.error) {
            NSLog(@"error:  %@",responseObject.error);
            [SVProgressHUD showInfoWithStatus:responseObject.error.description];
            [self.tableview.header endRefreshing];
            return ;
        }
        if (responseObject.code == 0) {
            JZHomeShopModel *homeShopM = responseObject.data;
            _likeArray = homeShopM.tuan_list;

             [weakself.tableview reloadData];
            // 拿到当前的下拉刷新控件，结束刷新状态
//            [self.tableView.header endRefreshing];
        }else{
            
        }
    } failure:^(NSError *error){
        NSLog(@"获取 猜你喜欢 数据失败");
        // 拿到当前的下拉刷新控件，结束刷新状态
        [SVProgressHUD showErrorWithStatus:@"网络连接失败"];
        [self.tableview.header endRefreshing];
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_homepageM != nil) {
        NSInteger num = 5;
        if (_likeArray.count>0) {
            num = 1+_likeArray.count;
            
        }
        NSLog(@"%ld", num);
        return num;
    }else{
        return 0;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 184;
    }
    else if(indexPath.row == 1){
        return 172;
    }else if (indexPath.row == 2){
        return 80;
    }else if (indexPath.row == 3){
        if (_specialModel.block_2) {
            return [JZHomeBlock2Cell tableView:tableView heightForRowAtIndexPath:indexPath withArray:_specialModel.block_2];
        }else{
            return 5;
        }
    }else if (indexPath.row == 4){
        return 132;
    }else if (indexPath.row == 5){
        return 36;
    }else{
        return 96;
    }

    

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
    static NSString *identifier = @"cell";
    HomeMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HomeMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier menuArray:_categoryArray];
    }

        
//        cell.delegate = self;
    return cell;
    }
    else if (indexPath.row == 1)
    {
        static NSString *cellIndentifier = @"JZHomeJingxuanCell";
        JZHomeJingxuanCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil) {
            cell = [[JZHomeJingxuanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier listArray:_topenModel.list activeTimeArray:_topenModel.activetime];
            

        }
        cell.delegate = self;
//        if (_topenModel) {
//        [cell setListArray:_topenModel.list];
//        [cell setActiveTimeArray:_topenModel.activetime];
//            //        }
//        cell.textLabel.text = @"ew";
        
        
        return cell;
        
    }
    else if (indexPath.row == 2){
        static NSString *cellIndentifier = @"JZHomeNewCell";
        JZHomeNewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil) {
            NSLog(@"%@", _userBlockArray);
            
            cell = [[JZHomeNewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier menuArray:_userBlockArray];
            
        }
//        if (_specialModel.block_1) {
//            [cell setHomeNewDataDic:_specialModel.block_1];
//        }
//        
        return cell;
    }
    else if (indexPath.row == 3){
        static NSString *cellIndentifier = @"cell3";
        JZHomeBlock2Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil) {
            if (_specialModel.block_2) {
                cell = [[JZHomeBlock2Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier array:_specialModel.block_2];
            }else{
                cell = [[JZHomeBlock2Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
            }
        }
        
        
        //赋值
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row == 4){
        static NSString *cellIndentifier = @"cell4";
        JZAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil) {
            cell = [[JZAlbumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier frame:CGRectMake(0, 0, screen_width, 132)];
        }
        if (_nuomiChannelArray.count) {
            [cell setSpecialArray:_nuomiChannelArray];
        }
        //赋值
        cell.delegate = self;
        return cell;
    }
    else if (indexPath.row == 5){
        static NSString *cellIndentifier = @"normalCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        }
        
        cell.textLabel.text = @"猜你喜欢";
        //赋值
        return cell;
    }else{
        static NSString *cellIndentifier = @"JZHomeShopCell";
        JZHomeShopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil) {
            cell = [[JZHomeShopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];

        }

        if (_likeArray.count>0) {
            JZShopTuanModel *shopM = _likeArray[indexPath.row -3];

            
            [cell setShopM:shopM];
        }
        //赋值
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        NSLog(@"2");
    }
    if (indexPath.row == 6) {
        NSLog(@"ds");
    }
//    else
//    {
////        if (indexPath.row == _likeArray.count) {
//        
//            NSLog(@"%@", _likeArray[indexPath.row - 7]);
//            
//            
////        }
//    }

}
-(void)didSelectedAlbumAtIndex:(NSInteger)index;

{
    NSLog(@"%ld",index);
    
}
- (void)didSelectedHomeBlock2AtIndex:(NSInteger)index
{
    NSLog(@"%ld", index);
    
}
-(void)homeMenuDidSelectedAtIndex:(NSInteger)index{
    
//     [self performSegueWithIdentifier:@"pushToMovie" sender:nil];
    if (index == 10) {
        JZHomeViewController *movie = [[JZHomeViewController alloc]init];
        [self.navigationController pushViewController:movie animated:NO];
        
 
    }
    
//
}
- (void)homeJingxuanDidSelectedAtIndex:(NSInteger)index
{
    NSLog(@"%ld", index);
    
}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//}
//

- (void)initData
{
    self.likeArray = [[NSMutableArray alloc] init];
    self.userBlockArray = [[NSMutableArray alloc]init];
    self.nuomiChannelArray = [[NSMutableArray alloc]init];
    
    self.bannersArray = [[NSMutableArray alloc] init];
    self.categoryArray = [[NSMutableArray alloc] init];
    self.recommendArray = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
